package com.suirich.dao.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.suirich.bean.Product;
import com.suirich.common.Constants;
import com.suirich.common.Page;
import com.suirich.dao.ProductDao;

@Repository
public class ProductDaoImpl extends GenericDaoHibernateImpl<Product, Long> implements ProductDao {
	public ProductDaoImpl() {
		super(Product.class);
	}

	@SuppressWarnings("unchecked")
	public List<Product> getProductList(Page page) {
		String hql = "select p.id as id,p.number as number,p.name as name,p.spec as spec,p.imagePath as imagePath," +
				"p.createDate as createDate,p.productCategories as productCategories,p.newRelease as newRelease " +
				"from Product p order by p.updateDate desc,p.createDate desc";
		
        Query query = this.getCurrentSession().createQuery(hql)
                .setResultTransformer(Transformers.aliasToBean(Product.class));

        if (page != null) {
            query.setFirstResult((page.getCurrentPage() - 1)
                    * page.getPerPageSize());
            query.setMaxResults(page.getPerPageSize());
        }
        
		return (List<Product>)query.list();
	}

	public Long getProductListCount() {
		String hql = "select count(p.id) from Product p";
		
		Query query = this.getCurrentSession().createQuery(hql);
		
		return (Long)query.uniqueResult();
	}

    @SuppressWarnings("unchecked")
    public List<Product> getProductListByCondition(String newRelease,List<Long> categories,Page page) {
        String categoryCondition = "";
        if(CollectionUtils.isNotEmpty(categories)){
            categoryCondition = " and (";
            for(Long category : categories){
                categoryCondition += " p.productCategories.id = " + category.toString() + " or";
            }
            categoryCondition = StringUtils.removeEnd(categoryCondition, "or");
            categoryCondition += ") ";
        }else{
            categoryCondition = " and (p.productCategories.id = 0) ";
        }
        
        String hql = "select p.id as id,p.name as name,p.imagePath as imagePath from Product p " +
        		"where p.newRelease" + (Constants.PRODUCT_IS_NEW_RELEASE.equals(newRelease)?"=":"!=") + ":newRelease " + categoryCondition +
        		"order by p.createDate desc";
        
        Query query = this.getCurrentSession().createQuery(hql)
                .setResultTransformer(Transformers.aliasToBean(Product.class));
        query.setString("newRelease", newRelease);
        
        if (page != null) {
            query.setFirstResult((page.getCurrentPage() - 1)
                    * page.getPerPageSize());
            query.setMaxResults(page.getPerPageSize());
        }
        
        return (List<Product>)query.list();
    }

    public Long getProductListByConditionCount(String newRelease,
            List<Long> categories) {
        String categoryCondition = "";
        if(CollectionUtils.isNotEmpty(categories)){
            categoryCondition = " and (";
            for(Long category : categories){
                categoryCondition += " p.productCategories.id = " + category.toString() + " or";
            }
            categoryCondition = StringUtils.removeEnd(categoryCondition, "or");
            categoryCondition += ") ";
        }else{
            categoryCondition = " and (p.productCategories.id = 0) ";
        }
        
        String hql = "select count(p.id) from Product p " +
                "where p.newRelease" + (Constants.PRODUCT_IS_NEW_RELEASE.equals(newRelease)?"=":"!=") + ":newRelease " + categoryCondition +
                "order by p.createDate desc";
        
        Query query = this.getCurrentSession().createQuery(hql);
        query.setString("newRelease", newRelease);
        
        return (Long)query.uniqueResult();
    }

    public void deleteProductsByCategoryId(Long categoryId) {
        String hql = "delete from Product p where p.productCategories.id = :categoryId";
        
        Query query = this.getCurrentSession().createQuery(hql);
        query.setLong("categoryId", categoryId);
        
        query.executeUpdate();
    }
}
